{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = r'./使用数据'       \n",
    "out_path  = r'./输出数据'  \n",
    "name_list = os.listdir(file_path)  \n",
    "#name_list.sort(key = lambda i:int(re.search(\"([a-z]*)([0-9]*)\",i).group(2)))  \n",
    "out_df = {'plot':[],'平均胸径':[],'算术平均胸径':[],'加权平均树高':[]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "z = 0\n",
    "for i in name_list:\n",
    "    z+=1\n",
    "    out_df['plot'].append(i)\n",
    "    data = os.path.join(file_path,i)\n",
    "    df = pd.read_csv(data,encoding ='utf_8_sig')\n",
    "    average_DBH = np.mean(df['胸径'])\n",
    "    tree_names = list(df['树种'].unique())\n",
    "    count_all = len(df)\n",
    "    listBins = [0,1,3,5,7,9,11,13,15,17,19,21]\n",
    "    listLabels = ['0','2','4','6','8','10','12','14','16','18','20']\n",
    "    df['径阶'] = None\n",
    "    df['径阶'] = pd.cut(df['胸径'],bins=listBins, labels=listLabels, include_lowest=True)\n",
    "    high=[]\n",
    "    area = []\n",
    "    dbh=[]\n",
    "    for i in np.unique(df['径阶']):\n",
    "        h = np.mean(df.loc[df['径阶']==i]['树高'].values)        \n",
    "        d = np.square(int(i)/2/100)*3.14\n",
    "        a = len(df[df['径阶']==i])\n",
    "        b = np.square(int(i))*a\n",
    "        dbh.append(b)\n",
    "        area.append(d)\n",
    "        c = h*d\n",
    "        high.append(c)\n",
    "    H = np.sum(high)/np.sum(area)\n",
    "    D = np.sqrt(np.sum(dbh)/count_all)    \n",
    "    out_df['平均胸径'].append(D)\n",
    "    out_df['算术平均胸径'].append(average_DBH)\n",
    "    out_df['加权平均树高'].append(H)    \n",
    "    out_df = pd.DataFrame(out_df)\n",
    "out_df.to_csv(os.path.join(out_path,'标准地计算.csv'),encoding = 'utf_8_sig',index=False) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>plot</th>\n",
       "      <th>平均胸径</th>\n",
       "      <th>算术平均胸径</th>\n",
       "      <th>加权平均树高</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>样地数据.csv</td>\n",
       "      <td>15.277434</td>\n",
       "      <td>14.9095</td>\n",
       "      <td>13.678251</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       plot       平均胸径   算术平均胸径     加权平均树高\n",
       "0  样地数据.csv  15.277434  14.9095  13.678251"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "out_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
